package com.mediatecture.flex.component.SaveToPDF
{
	import Controller.AreasController;
	import Controller.HeatingController;
	import Controller.QualityController;
	
	import Model.Calculations;
	import Model.HeatRecoverySystem;
	import Model.SolidComponent;
	import Model.SubsoilHeatExchanger;
	import Model.TransparentComponent;
	
	import View.Output.SetupSynopsis;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.globalization.NumberFormatter;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	import mx.collections.ArrayCollection;
	import mx.core.FlexGlobals;
	
	import org.alivepdf.events.PageEvent;
	import org.alivepdf.fonts.CoreFont;
	import org.alivepdf.fonts.FontFamily;
	import org.alivepdf.layout.Mode;
	import org.alivepdf.layout.Orientation;
	import org.alivepdf.layout.Position;
	import org.alivepdf.layout.Resize;
	import org.alivepdf.layout.Size;
	import org.alivepdf.layout.Unit;
	import org.alivepdf.pages.Page;
	import org.alivepdf.pdf.PDF;
	import org.alivepdf.saving.Method;

	public class SaveToPDF extends Sprite
	{
		private var file:File;
		private var pdf:PDF;
		private var resize:Resize;
		private var resize1:Resize;
		
		private var DO:DisplayObject;
		private var DO1:DisplayObject;
		private var header:DisplayObject;
		
		private var areasController:AreasController;
		private var quantityController:QualityController;
		private var heatingController:HeatingController;
		private var numberFormatter:NumberFormatter;
		
		private var projecDataPage:ArrayCollection;
		private var qualityPage:ArrayCollection;
		private var solidComponentsPages:ArrayCollection;
		private var doorsPages:ArrayCollection;
		private var windowsPages:ArrayCollection;
		private var heatingPage:ArrayCollection;
		private var calculationsPage:ArrayCollection;
		
		//private var myCoreFont:IFont;
		
		public function SaveToPDF()
		{
			this.projecDataPage = new ArrayCollection();
			this.qualityPage = new ArrayCollection();
			this.solidComponentsPages = new ArrayCollection();
			this.doorsPages = new ArrayCollection();
			this.windowsPages = new ArrayCollection();
			this.heatingPage = new ArrayCollection();
			this.calculationsPage = new ArrayCollection();
			
			this.areasController = new AreasController();
			this.quantityController = new QualityController();
			this.heatingController = new HeatingController();
			
			//this.myCoreFont = new CoreFont(FontFamily.HELVETICA_BOLD);
				
			this.file = new File();
			this.file = file.resolvePath("/New PDF.pdf");
			this.pdf = new PDF(Orientation.PORTRAIT, Unit.MM, Size.A4);
			file.addEventListener(Event.SELECT, save);
			pdf.addEventListener(PageEvent.ADDED, addHeader);
			
			this.resize = new Resize(Mode.FIT_TO_PAGE, Position.LEFT);
			this.resize1 = new Resize(Mode.NONE, Position.LEFT);
			
		}
		
		public function print():void
		{
			
		}
		
		public function startPDF():void
		{
			file.browseForSave("Save as PDF");
			addRowsImages();
		}
		
		private function addRowsImages():void
		{
			var loader:Loader = new Loader();
			loader.name = "hui";
			addChild(loader);
			
			loader.load(new URLRequest("img/pdf-white.jpg"));
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function f(e:Event):void
			{
				DO = e.target.loader;
				
				var loader1:Loader = new Loader();
				addChild(loader1);
				
				loader1.load(new URLRequest("img/pdf-grey.jpg"));
				loader1.contentLoaderInfo.addEventListener(Event.COMPLETE, function f(e:Event):void
				{
					DO1 = e.target.loader;
					
					var loader2:Loader = new Loader();
					addChild(loader2);
					
					loader2.load(new URLRequest("img/pdf-header.png"));
					loader2.contentLoaderInfo.addEventListener(Event.COMPLETE, function f(e:Event):void
					{
						header = e.target.loader;
						
						createPDF();
						printAreaVolumeQualityShading();
						printSolidComponents();
						printWindows();
						printDoors();
						printHVAC();
						printCalculations();
					});
				});
			});
		}
		
		private function newWhiteRow(y:Number, strTitle:String, strValue:String):void
		{
			pdf.addImage(DO, resize1, 0, y);
			pdf.setFont(new CoreFont());
			pdf.setFontSize(8);
			pdf.addCell(70, 12, strTitle);
			pdf.addCell(15, 12, strValue, 0,0,"R");
			pdf.newLine(8);
		}
		
		private function newHeatingDescriptionRow(y:Number, strTitle:String, strValue:String):void
		{
			pdf.addImage(DO, resize1, 0, y);
			pdf.setFontSize(8);
			pdf.addCell(70, 12, strTitle);
			pdf.addCell(50, 12, strValue);
			pdf.newLine(8);
		}
		
		private function newThreeLabelRow(y:Number, strTitle:String, strValue:String, unit:String):void
		{
			pdf.addImage(DO, resize1, 0, y);
			pdf.setFont(new CoreFont());
			pdf.setFontSize(8);
			pdf.addCell(70, 12, strTitle);
			pdf.addCell(15, 12, strValue, 0,0,"R");
			pdf.addCell(20, 12, unit);
			pdf.newLine(8);
		}
		
		private function newGreyRow(y:Number, strTitle:String):void
		{
			pdf.addImage(DO1, resize1, 0, y);
			pdf.setFont(new CoreFont(FontFamily.HELVETICA_BOLD));
			pdf.setFontSize(10);
			pdf.writeText(10, strTitle);
			pdf.newLine(8);
		}
		
		private function newEmptyLine(y:Number):void
		{
			pdf.addImage(DO, resize1, 0, y);
			pdf.newLine(8);
		}
		
		private function thermalBridgeYesNo(boo:Boolean):String
		{
			var str:String;
			
			if(boo == true)
			{
				str = "Yes";
			}
			else
			{
				str = "No";
			}
			
			return str;
		}
		
		private function printAreaVolumeQualityShading():void
		{
			
			pdf.addPage();
			
			pdf.setFont(new CoreFont(FontFamily.HELVETICA_BOLD));
			
			pdf.setFontSize(10);
			pdf.writeText(10, "HEATED AREA AND VOLUME");
			pdf.newLine(11);
			
			newGreyRow(32, "    Total Heated Space Area");
			newThreeLabelRow(40, "    Heated Space Area: ", Number(FlexGlobals.topLevelApplication.getHSA().sum).toFixed(2), " m²");
			newEmptyLine(48);
			newGreyRow(56, "    Total Heated Space Volume");
			newThreeLabelRow(64, "    Heated Space Volume: ", Number(FlexGlobals.topLevelApplication.getHSVArea()).toFixed(2), " m³");
			newEmptyLine(72);
			
			pdf.newLine(8);
			
			pdf.setFont(new CoreFont(FontFamily.HELVETICA_BOLD));
			
			pdf.setFontSize(10);
			pdf.writeText(10, "QUANTITY");
			pdf.newLine(11);
			
			newGreyRow(99, "    Airtightness");
			newWhiteRow(107, "    Airtightness Rate: ", this.quantityController.getAirtightnessRate().toString());
			newEmptyLine(115);
			newGreyRow(123, "    Thermal Bridges");
			newWhiteRow(131, "    Thermal Bridge - Free: ", thermalBridgeYesNo(this.quantityController.getThermalBridgeFree()));
			newEmptyLine(139);
			
			pdf.newLine(8);
			
			pdf.setFont(new CoreFont(FontFamily.HELVETICA_BOLD));
			
			pdf.setFontSize(10);
			pdf.writeText(10, "SHADING");
			pdf.newLine(11);
			
			newGreyRow(166, "    Mean Shading Factors");
			newWhiteRow(174, "    North: ", (FlexGlobals.topLevelApplication.shadingFromBalconies.shadingNorth.shadingFactor * 
				FlexGlobals.topLevelApplication.shadingFromNeighbouringHouses.shadingNorth.shadingFactor * 
				FlexGlobals.topLevelApplication.shadingFromTrees.shadingNorth.shadingFactor).toFixed(2));
			
			newWhiteRow(182, "    East: ", (FlexGlobals.topLevelApplication.shadingFromBalconies.shadingEast.shadingFactor * 
				FlexGlobals.topLevelApplication.shadingFromNeighbouringHouses.shadingEast.shadingFactor * 
				FlexGlobals.topLevelApplication.shadingFromTrees.shadingEast.shadingFactor).toFixed(2));
			
			newWhiteRow(190, "    South: ", (FlexGlobals.topLevelApplication.shadingFromBalconies.shadingSouth.shadingFactor * 
				FlexGlobals.topLevelApplication.shadingFromNeighbouringHouses.shadingSouth.shadingFactor * 
				FlexGlobals.topLevelApplication.shadingFromTrees.shadingSouth.shadingFactor).toFixed(2));
			
			newWhiteRow(198, "    West: ", (FlexGlobals.topLevelApplication.shadingFromBalconies.shadingWest.shadingFactor * 
				FlexGlobals.topLevelApplication.shadingFromNeighbouringHouses.shadingWest.shadingFactor * 
				FlexGlobals.topLevelApplication.shadingFromTrees.shadingWest.shadingFactor).toFixed(2));
			
			newEmptyLine(206);
			
			for(var i:Number = 0; i < pdf.getPages().length; i++)
			{
				this.qualityPage.addItem(pdf.getPage(i + 1));
			}
			pdf.removeAllPages();
		}
		
		private function printSC(desc:String, imgPath:String, id:String, length:String, height:String, area:String, name:String, dimensions:String, layers:String):void
		{
			var loader:Loader = new Loader();
			addChild(loader);
			
			loader.load(new URLRequest(imgPath));
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function f(e:Event):void
			{
				pdf.addPage();
				
				pdf.setFont(new CoreFont(FontFamily.HELVETICA_BOLD));
				
				pdf.setFontSize(10);
				pdf.writeText(10, "AREAS");
				pdf.newLine(8);
				pdf.writeText(10, "Solid Components");
				pdf.newLine(11);
				
				newGreyRow(40, "    " + desc);
				newWhiteRow(48, "    ID: ", id);
				newThreeLabelRow(56, "    Length: ", length," m");
				newThreeLabelRow(64, "    Height: ", height, " m");
				newThreeLabelRow(72, "    Area: ", area, " m²");
				newWhiteRow(80, "    ", "");
				newEmptyLine(88);
				newEmptyLine(96);
				newEmptyLine(104);
				newEmptyLine(112);
				newEmptyLine(120);
				newEmptyLine(128);
				newEmptyLine(136);
				newEmptyLine(144);
				newEmptyLine(152);
				newEmptyLine(160);
				
				pdf.addImage(e.target.loader, resize1, 10, 96);
				
				newGreyRow(168, "    Cut Info");
				
				pdf.setFont(new CoreFont());
				pdf.setFontSize(8);
				
				newEmptyLine(176);
				
				var pattern:RegExp = /<br>/g;
				var returns:RegExp = /\r\n/g;
				
				dimensions = dimensions.replace(returns, "");
				layers = layers.replace(returns, "");
				
				dimensions = dimensions.replace(pattern, "hui");
				layers = layers.replace(pattern, "hui");
				
				var arrD:Array = new Array();
				var arrN:Array = new Array();
				
				arrD = dimensions.split("hui");
				arrN = layers.split("hui");
				
				var obj:Object;
				
				var arrayWithDimensions:Array = new Array();
				var arrayWithNames:Array = new Array();
				
				var j:Number = 0;
				var k:Number = 0;
				
				for each(var str:String in arrD)
				{	
					obj = new Object();
					
					obj.index = j;
					obj.dimensions = str;
					
					arrayWithDimensions.push(obj);
					
					j++;
				}
				
				for each(var str:String in arrN)
				{
					obj = new Object();
					
					obj.index = k;
					obj.names = str;
					
					arrayWithNames.push(obj);
					
					k++
				}
				
				for each(var dimension:Object in arrayWithDimensions)
				{
					var index:Number = dimension.index;
					
					pdf.addCell(20, 5, "    " + dimension.dimensions);
					
					for each(var name:Object in arrayWithNames)
					{
						if(index == name.index)
						{
							pdf.addCell(100, 5, name.names);
						}
					}
					pdf.newLine(5);
				}
			
				
				for(var i:Number = 0; i < pdf.getPages().length; i++)
				{
					solidComponentsPages.addItem(pdf.getPage(i + 1));
				}
				
				pdf.removeAllPages();

			});
		}
		
		private function printWin(desc:String, imgPath:String, id:String, length:String, height:String, area:String, name:String, dimensions:String, layers:String):void
		{
			var loader:Loader = new Loader();
			addChild(loader);
			
			loader.load(new URLRequest(imgPath));
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function f(e:Event):void
			{
				pdf.addPage();
				
				pdf.setFont(new CoreFont(FontFamily.HELVETICA_BOLD));
					
				pdf.setFontSize(10);
				pdf.writeText(10, "AREAS");
				pdf.newLine(8);
				pdf.writeText(10, "Transparent Components");
				pdf.newLine(11);
					
				newGreyRow(40, "    " + desc);
				newWhiteRow(48, "    ID: ", id);
				newThreeLabelRow(56, "    Length: ", length, " m");
				newThreeLabelRow(64, "    Height: ", height, " m");
				newThreeLabelRow(72, "    Area: ", area, " m²");
				newWhiteRow(80, "    " + name, "");
				newEmptyLine(88);
				newEmptyLine(96);
				newEmptyLine(104);
				newEmptyLine(112);
				newEmptyLine(120);
				newEmptyLine(128);
				newEmptyLine(136);
				newEmptyLine(144);
				newEmptyLine(152);
				newEmptyLine(160);
					
				pdf.addImage(e.target.loader, resize1, 10, 96);
				
				newGreyRow(168, "    Cut Info");
				
				pdf.setFont(new CoreFont());
				pdf.setFontSize(8);
				
				newEmptyLine(176);
				
				var pattern:RegExp = /<br>/g;
				var returns:RegExp = /\r\n/g;
				
				dimensions = dimensions.replace(returns, "");
				layers = layers.replace(returns, "");
				
				dimensions = dimensions.replace(pattern, "hui");
				layers = layers.replace(pattern, "hui");
				
				var arrD:Array = new Array();
				var arrN:Array = new Array();
				
				arrD = dimensions.split("hui");
				arrN = layers.split("hui");
				
				var obj:Object;
				
				var arrayWithDimensions:Array = new Array();
				var arrayWithNames:Array = new Array();
				
				var j:Number = 0;
				var k:Number = 0;
				
				for each(var str:String in arrD)
				{	
					obj = new Object();
					
					obj.index = j;
					obj.dimensions = str;
					
					arrayWithDimensions.push(obj);
					
					j++;
				}
				
				for each(var str:String in arrN)
				{
					obj = new Object();
					
					obj.index = k;
					obj.names = str;
					
					arrayWithNames.push(obj);
					
					k++
				}
				
				for each(var dimension:Object in arrayWithDimensions)
				{
					var index:Number = dimension.index;
					
					pdf.addCell(20, 5, "    " + dimension.dimensions);
					
					for each(var name:Object in arrayWithNames)
					{
						if(index == name.index)
						{
							pdf.addCell(100, 5, name.names);
						}
					}
					pdf.newLine(5);
				}
				
				for(var i:Number = 0; i < pdf.getPages().length; i++)
				{
					windowsPages.addItem(pdf.getPage(i + 1));
				}
				
				pdf.removeAllPages();
			});
		}
		
		private function printDoor(desc:String, imgPath:String, id:String, length:String, height:String, area:String, name:String, dimensions:String, layers:String):void
		{
			var loader:Loader = new Loader();
			addChild(loader);
			
			loader.load(new URLRequest(imgPath));
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function f(e:Event):void
			{
				pdf.addPage();
				
				pdf.setFont(new CoreFont(FontFamily.HELVETICA_BOLD));
				
				pdf.setFontSize(10);
				pdf.writeText(10, "AREAS");
				pdf.newLine(8);
				pdf.writeText(10, "Transparent Components");
				pdf.newLine(11);
				
				newGreyRow(40, "    " + desc);
				newWhiteRow(48, "    ID: ", id);
				newThreeLabelRow(56, "    Length: ", length, " m");
				newThreeLabelRow(64, "    Height: ", height, " m");
				newThreeLabelRow(72, "    Area: ", area, " m²");
				newWhiteRow(80, "    " + name, "");
				newEmptyLine(88);
				newEmptyLine(96);
				newEmptyLine(104);
				newEmptyLine(112);
				newEmptyLine(120);
				newEmptyLine(128);
				newEmptyLine(136);
				newEmptyLine(144);
				newEmptyLine(152);
				newEmptyLine(160);
				
				pdf.addImage(e.target.loader, resize1, 10, 96);
				
				newGreyRow(168, "    Cut Info");
				
				pdf.setFont(new CoreFont());
				pdf.setFontSize(8);
				
				newEmptyLine(176);
				
				var pattern:RegExp = /<br>/g;
				var returns:RegExp = /\r\n/g;
				
				dimensions = dimensions.replace(returns, "");
				layers = layers.replace(returns, "");
				
				dimensions = dimensions.replace(pattern, "hui");
				layers = layers.replace(pattern, "hui");
				
				var arrD:Array = new Array();
				var arrN:Array = new Array();
				
				arrD = dimensions.split("hui");
				arrN = layers.split("hui");
				
				var obj:Object;
				
				var arrayWithDimensions:Array = new Array();
				var arrayWithNames:Array = new Array();
				
				var j:Number = 0;
				var k:Number = 0;
				
				for each(var str:String in arrD)
				{	
					obj = new Object();
					
					obj.index = j;
					obj.dimensions = str;
					
					arrayWithDimensions.push(obj);
					
					j++;
				}
				
				for each(var str:String in arrN)
				{
					obj = new Object();
					
					obj.index = k;
					obj.names = str;
					
					arrayWithNames.push(obj);
					
					k++
				}
				
				for each(var dimension:Object in arrayWithDimensions)
				{
					var index:Number = dimension.index;
					
					pdf.addCell(20, 5, "    " + dimension.dimensions);
					
					for each(var name:Object in arrayWithNames)
					{
						if(index == name.index)
						{
							pdf.addCell(100, 5, name.names);
						}
					}
					pdf.newLine(5);
				}
				
				for(var i:Number = 0; i < pdf.getPages().length; i++)
				{
					doorsPages.addItem(pdf.getPage(i + 1));
				}
				
				pdf.removeAllPages();
			});
		}
		
		private function printHVAC():void
		{
			var heatRecoverySystem:HeatRecoverySystem = this.heatingController.getHeatRecoverySystem();
			var subsoilHeatExchanger:SubsoilHeatExchanger = this.heatingController.getSubsoilHeatExchanger();
			
			if(heatRecoverySystem.efficiency != 0)
			{
				var loader:Loader = new Loader();
				addChild(loader);
				
				loader.load(new URLRequest(heatRecoverySystem.pictureURL));
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function f(e:Event):void
				{
					pdf.addPage();
					
					pdf.setFont(new CoreFont(FontFamily.HELVETICA_BOLD));
						
					pdf.setFontSize(10);
					pdf.writeText(10, "HVAC");
					pdf.newLine(10);
					
					newGreyRow(30, "    Heat Recovery System");
					newWhiteRow(38, "    Heat Recovery System Type: ", heatRecoverySystem.type.toString());
					newHeatingDescriptionRow(46, "    Description: ", heatRecoverySystem.description);
					newThreeLabelRow(54, "    Efficiency: ", heatRecoverySystem.efficiency.toString(), " %");
					newEmptyLine(62);
					newEmptyLine(70);
					newEmptyLine(78);
					newEmptyLine(86);
					newEmptyLine(94);
					newEmptyLine(102);
					newEmptyLine(110);
					newEmptyLine(118);
					newEmptyLine(126);
					newEmptyLine(134);
						
					pdf.addImage(e.target.loader, resize1, 10, 70);
	
					if(!isNaN(subsoilHeatExchanger.efficiency))
					{
						newGreyRow(142, "    Subsoil Heat Exchanger");
						newThreeLabelRow(150, "    Length: ", subsoilHeatExchanger.length.toString(), " m");
						newThreeLabelRow(158, "    Efficiency: ", subsoilHeatExchanger.efficiency.toString(), " %");
						newEmptyLine(166);
					}
					for(var i:Number = 0; i < pdf.getPages().length; i++)
					{
						heatingPage.addItem(pdf.getPage(i + 1));
					}
					
					pdf.removeAllPages();
				});
			}
		}
		
		private function printCalculations():void
		{
			var calculations:Calculations = FlexGlobals.topLevelApplication.calculations;
			
			pdf.addPage();
			
			pdf.setFont(new CoreFont(FontFamily.HELVETICA_BOLD));
			
			pdf.setFontSize(10);
			pdf.writeText(10, "CALCULATIONS");
			pdf.newLine(10);
			
			newGreyRow(30, "    Calculations");
			newThreeLabelRow(38, "    Transmission Heat Losses: ", calculations.transmissionHeatLosses.toFixed(2), " kWh/a");
			newThreeLabelRow(46, "    Ventilation Heat Losses: ", calculations.ventilationHeatLosses.toFixed(2), " kWh/a");
			newThreeLabelRow(54, "    Total Heat Losses: ", calculations.totalHeatLosses.toFixed(2), " kWh/a");
			newThreeLabelRow(62, "    Internal Heat Gains: ", calculations.internalHeatGains.toFixed(2), " kWh/a");
			newThreeLabelRow(70, "    Available Solar Heat Gains: ", calculations.solarHeatGains.toFixed(2), " kWh/a");
			newThreeLabelRow(78, "    Total Heat Gains: ", calculations.totalHeatGains.toFixed(2), " kWh/a");
			newThreeLabelRow(86, "    Annual Heat Demand: ", calculations.annualHeatDemand.toFixed(2), " kWh/a");
			newThreeLabelRow(94, "    Specific Annual Heat Demand: ", calculations.specificAnnualHeatDemand.toFixed(2), " kWh/(m²a)");
			newEmptyLine(102);
			
			
			for(var i:Number = 0; i < pdf.getPages().length; i++)
			{
				this.calculationsPage.addItem(pdf.getPage(i + 1));
			}
			
			pdf.removeAllPages();
		}
		
		private function printSolidComponents():void
		{
			for each(var solidcomponent:SolidComponent in this.areasController.getAllSolidComponentsWithAttachedConstruction())
			{
				printSC(solidcomponent.description, solidcomponent.construction.imageURL, solidcomponent.id, solidcomponent.length.toFixed(2), solidcomponent.height.toFixed(2),
							solidcomponent.area.toFixed(2), solidcomponent.construction.name, 
							solidcomponent.construction.cutInfo.dimensions, solidcomponent.construction.cutInfo.layersNames);
			}
		}
		
		private function printWindows():void
		{
			for each(var window:TransparentComponent in this.areasController.getWindowsWithAttachedConstruction())
			{
				printWin(window.description, window.construction.imageURL, window.id, window.length.toFixed(2),
							window.height.toFixed(2), window.area.toFixed(2), window.construction.name, 
							window.construction.cutInfo.dimensions, window.construction.cutInfo.layersNames);
			}
		}
		
		private function printDoors():void
		{
			for each(var door:TransparentComponent in this.areasController.getDoorsWithAttachedConstruction())
			{
				printDoor(door.description, door.construction.imageURL, door.id, door.length.toFixed(2),
					door.height.toFixed(2), door.area.toFixed(2), door.construction.name, 
					door.construction.cutInfo.dimensions, door.construction.cutInfo.layersNames);
			}
		}
		
		private function createPDF():void
		{
			pdf.addPage();

			pdf.setLeftMargin(20);
			pdf.setRightMargin(20);
			
			pdf.setFont(new CoreFont(FontFamily.HELVETICA_BOLD));
			
			pdf.setFontSize(10);
			pdf.writeText(10, "PROJECT DATA");
			
			pdf.newLine(10);
			
			pdf.addImage(DO1, resize1, 0, 30);
			
			//Subtitle
			pdf.setFontSize(10);
			pdf.writeText(10, "    Addresses");
			pdf.newLine(8);
			
			pdf.setFont(new CoreFont());
			
			//Object data
			pdf.setFontSize(8);
			pdf.addImage(DO, resize1, 0, 39);
			pdf.addCell(70, 12, "    Object: ");
			pdf.addCell(70, 12, FlexGlobals.topLevelApplication.project.projectInfo.object.name);
			pdf.newLine(8);
			pdf.addImage(DO, resize1, 0, 47);
			pdf.addCell(70, 12, "    Street: ");
			pdf.addCell(70, 12, FlexGlobals.topLevelApplication.project.projectInfo.object.address.street);
			pdf.newLine(8);
			pdf.addImage(DO, resize1, 0, 55);
			pdf.addCell(70, 12, "    ZIP/Town: ");
			pdf.addCell(70, 12, FlexGlobals.topLevelApplication.project.projectInfo.object.address.town);
			pdf.newLine(8);
			pdf.addImage(DO, resize1, 0, 63);
			pdf.addCell(70, 12, "    Country: ");
			pdf.addCell(70, 12, FlexGlobals.topLevelApplication.project.projectInfo.object.address.country);
			pdf.newLine(8);
			pdf.addImage(DO, resize1, 0, 71);
			pdf.newLine(8);
			
			//Builder data
			pdf.addImage(DO, resize1, 0, 79);
			pdf.addCell(70, 12, "    Builder: ");
			pdf.addCell(70, 12, FlexGlobals.topLevelApplication.project.projectInfo.builder.name);
			pdf.newLine(8);
			pdf.addImage(DO, resize1, 0, 87);
			pdf.addCell(70, 12, "    Street: ");
			pdf.addCell(70, 12, FlexGlobals.topLevelApplication.project.projectInfo.builder.address.street);
			pdf.newLine(8);
			pdf.addImage(DO, resize1, 0, 95);
			pdf.addCell(70, 12, "    ZIP/Town: ");
			pdf.addCell(70, 12, FlexGlobals.topLevelApplication.project.projectInfo.builder.address.town);
			pdf.newLine(8);
			pdf.addImage(DO, resize1, 0, 103);
			pdf.addCell(70, 12, "    Country: ");
			pdf.addCell(70, 12, FlexGlobals.topLevelApplication.project.projectInfo.builder.address.country);
			pdf.newLine(8);
			pdf.addImage(DO, resize1, 0, 111);
			pdf.newLine(8);
			
			//Architect data
			pdf.addImage(DO, resize1, 0, 119);
			pdf.addCell(70, 12, "    Architect: ");
			pdf.addCell(70, 12, FlexGlobals.topLevelApplication.project.projectInfo.architect.name);
			pdf.newLine(8);
			pdf.addImage(DO, resize1, 0, 127);
			pdf.addCell(70, 12, "    Street: ");
			pdf.addCell(70, 12, FlexGlobals.topLevelApplication.project.projectInfo.architect.address.street);
			pdf.newLine(8);
			pdf.addImage(DO, resize1, 0, 135);
			pdf.addCell(70, 12, "    ZIP/Town: ");
			pdf.addCell(70, 12, FlexGlobals.topLevelApplication.project.projectInfo.architect.address.town);
			pdf.newLine(8);
			pdf.addImage(DO, resize1, 0, 143);
			pdf.addCell(70, 12, "    Country: ");
			pdf.addCell(70, 12, FlexGlobals.topLevelApplication.project.projectInfo.architect.address.country);
			pdf.newLine(8);
			pdf.addImage(DO, resize1, 0, 151);
			pdf.addCell(70, 12, "    Notes: ");
			pdf.addCell(70, 12, FlexGlobals.topLevelApplication.project.projectInfo.notes);
			pdf.newLine(8);
			pdf.addImage(DO, resize1, 0, 159);
			pdf.newLine(8);
			
			//Climate data
			pdf.setFont(new CoreFont(FontFamily.HELVETICA_BOLD));
			
			pdf.addImage(DO1, resize1, 0, 167);
			pdf.setFontSize(10);
			pdf.writeText(10, "    Climate");
			pdf.newLine(8);
			
			pdf.setFont(new CoreFont());
			
			pdf.addImage(DO, resize1, 0, 175);
			pdf.setFontSize(8);
			pdf.addCell(70, 12, "    Climate zone: ");
			if(FlexGlobals.topLevelApplication.project.projectInfo.climateZone.label != null)
			{
				pdf.addCell(70, 12, FlexGlobals.topLevelApplication.project.projectInfo.climateZone.label);
			}
			pdf.newLine(8);
			pdf.addImage(DO, resize1, 0, 183);
			pdf.newLine(8);
			
			//Construction data
			pdf.setFont(new CoreFont(FontFamily.HELVETICA_BOLD));
			
			pdf.addImage(DO1, resize1, 0, 191);
			pdf.setFontSize(10);
			pdf.writeText(10, "    Type of Construction");
			pdf.newLine(8);
			
			pdf.setFont(new CoreFont());
			
			pdf.addImage(DO, resize1, 0, 199);
			pdf.setFontSize(8);
			pdf.addCell(70, 12, "    Construction: ");
			pdf.addCell(70, 12, FlexGlobals.topLevelApplication.project.projectInfo.typeConstruction);
			pdf.newLine(8);
			pdf.addImage(DO, resize1, 0, 207);
			pdf.newLine(8);
			
			//Usage data
			pdf.setFont(new CoreFont(FontFamily.HELVETICA_BOLD));
			
			pdf.addImage(DO1, resize1, 0, 215);
			pdf.setFontSize(10);
			pdf.writeText(10, "    Usage");
			pdf.newLine(8);
			
			pdf.setFont(new CoreFont());
			
			pdf.addImage(DO, resize1, 0, 223);
			pdf.setFontSize(8);
			pdf.addCell(70, 12, "    Building type: ");
			pdf.addCell(70, 12, FlexGlobals.topLevelApplication.project.projectInfo.buildingType);
			pdf.newLine(8);
			pdf.addImage(DO, resize1, 0, 231);
			pdf.addCell(70, 12, "    Usage: ");
			pdf.addCell(70, 12, FlexGlobals.topLevelApplication.project.projectInfo.usage);
			pdf.newLine(8);
			pdf.addImage(DO, resize1, 0, 239);
			pdf.addCell(70, 12, "    Interior temp.: ");
			pdf.addCell(70, 12, FlexGlobals.topLevelApplication.project.projectInfo.interiorTemperature + "°");
			pdf.newLine(8);
			
			for(var i:Number = 0; i < pdf.getPages().length; i++)
			{
				projecDataPage.addItem(pdf.getPage(i + 1));
			}
			pdf.removeAllPages();
		}
		
		protected function save(event:Event):void
		{
			var f:FileStream = new FileStream();
			f.open(file, FileMode.WRITE);
			
			var synopsisSettings:SetupSynopsis = FlexGlobals.topLevelApplication.setupSynopsis;
			
			if(synopsisSettings.projectDataChkBox.selected == true)
			{
				for each(var page:Page in this.projecDataPage)
				{
					pdf.addPage(page);	
				}
			}
			if(synopsisSettings.areasChkBox.selected == true)
			{	
				for each(var page:Page in this.qualityPage)
				{
					pdf.addPage(page);	
				}
			}
			if(synopsisSettings.solidComponentsChkBox.selected == true)
			{	
				for each(var page:Page in this.solidComponentsPages)
				{
					pdf.addPage(page);	
				}
			}
			if(synopsisSettings.windowsChkBox.selected == true)
			{
				for each(var page:Page in this.windowsPages)
				{
					pdf.addPage(page);	
				}
			}
			if(synopsisSettings.doorsChkBox.selected == true)
			{
				for each(var page:Page in this.doorsPages)
				{
					pdf.addPage(page);	
				}
			}
			if(synopsisSettings.heatingChkBox.selected == true)
			{
				for each(var page:Page in this.heatingPage)
				{
					pdf.addPage(page);	
				}
			}
			if(synopsisSettings.calculationsChkBox.selected == true)
			{
				for each(var page:Page in this.calculationsPage)
				{
					pdf.addPage(page);	
				}
			}
			var bytes:ByteArray = pdf.save(Method.LOCAL);
			f.writeBytes(bytes);
			f.close();
		}
		
		protected function addHeader(event:PageEvent):void
		{
			pdf.setLeftMargin(0);
			pdf.setRightMargin(0);
			pdf.addImage(header, resize);
			pdf.newLine(20);
			pdf.setLeftMargin(20);
		}
	}
}